package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.*;
import entity.*;

/**
 * Servlet implementation class ModifyUserServlet
 */
@WebServlet("/ModifyUserServlet")
public class ModifyUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		//response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		int type = 0;
		try {
			type = Integer.parseInt(request.getParameter("type"));
		} catch (NumberFormatException e) {
			// 如果浏览器没有传递start值
		}
		if(type==0)
		{
		User user = new User();
		user.setUser_id(Integer.parseInt(request.getParameter("user_id")));
		user.setUser_name(request.getParameter("user_name"));
		user.setUser_password(request.getParameter("user_password"));
		int sex = 0, islocked = 0;//默认性别女，状态正常
		if(request.getParameter("user_sex").equals("男")) sex=1;
		if(request.getParameter("user_islocked").equals("禁用")) islocked=1;
		user.setUser_sex(sex);
		user.setUser_islocked(islocked);
		user.setUser_email(request.getParameter("user_email"));
		user.setUser_tel(request.getParameter("user_tel"));
		
		UserDAO.getInstance().updateUser(user);
		
		//TODO 提示修改成功 的信息
		//response.sendRedirect("result_information.jsp?");
		request.setAttribute("result_information","修改成功！" );
		request.setAttribute("return_page","ManageUserServlet" );
		request.setAttribute("return_page_name","用户账号列表" );
		RequestDispatcher view = request.getRequestDispatcher("result_information.jsp?");
		view.forward(request, response);
		}
		else if(type==1)
		{
			String name = null;
			String email = null;
			String tel = null;
			int i= 0;
			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("user");
			try {
				name = (request.getParameter("user_name"));
			} catch (NumberFormatException e) {
				// 如果浏览器没有传递start值
			}
			try {
				email=(request.getParameter("user_email"));
			} catch (NumberFormatException e) {
				// 如果浏览器没有传递start值
			}
			try {
				tel=(request.getParameter("user_tel"));
			} catch (NumberFormatException e) {
				// 如果浏览器没有传递start值
			}
			if(name ==""&&email==""&&tel=="")
			{
				
			}
			else
			{
				if(name!="")
				{
					user.setUser_name(name);
					i=2;
				}
				if(email!="")
				{
					user.setUser_email(email);
					i=2;
				}
				if(tel!="")
				{
					user.setUser_tel(tel);
					i=2;
				}
				UserDAO.getInstance().updateUser(user);
			}
			
			request.setAttribute("i",i);
			request.getRequestDispatcher("ChangeInformation.jsp").forward(request, response);	
		}
	}

}
